package com.atguigu.yygh.orders.service.impl;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.enums.PaymentStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.atguigu.yygh.orders.mapper.PaymentInfoMapper;
import com.atguigu.yygh.orders.service.OrderInfoService;
import com.atguigu.yygh.orders.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 支付信息表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-09-14
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private OrderInfoService orderInfoService;

    //添加支付记录
    @Override
    //第二个参数为支付类型 1 微信 2 支付宝 这里只用微信
    public void savePaymentInfo(OrderInfo orderInfo, Integer paymentType) {
        //查询是否存在相同支付记录
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderInfo.getId());
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            return;
        }
        //2 不存在添加
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(paymentType); //支付类型
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());//流水号

        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());

        //交易内容
        String subject = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd") + "|" + orderInfo.getHosname() + "|" + orderInfo.getDepname() + "|" + orderInfo.getTitle();
        paymentInfo.setSubject(subject);
        //挂号费
        paymentInfo.setTotalAmount(orderInfo.getAmount());
        baseMapper.insert(paymentInfo);
    }

    //支付成功，更新状态
    @Override
    public void paySuccess(String outTradeNo, Integer paymentType, Map<String, String> paramMap) {
        //1 更新支付记录表数据状态
        QueryWrapper<PaymentInfo> wrapperPayMent = new QueryWrapper<>();
        wrapperPayMent.eq("out_trade_no", outTradeNo);
        wrapperPayMent.eq("payment_type", paymentType);
        PaymentInfo paymentInfo = baseMapper.selectOne(wrapperPayMent);
        //查询支付记录为空
        if (null == paymentInfo) {
            throw new YyghException();
        }
        //如果支付记录支付状态已经支付，不需要更新
        if (paymentInfo.getPaymentStatus() != PaymentStatusEnum.UNPAID.getStatus()) {
            return;
        }
        //设置修改值
        paymentInfo.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());
        paymentInfo.setTradeNo(paramMap.get("transaction_id"));
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(paramMap.toString());
        baseMapper.updateById(paymentInfo);

        //2 更新订单表订单支付状态
        OrderInfo orderInfo = orderInfoService.getById(paymentInfo.getOrderId());
        orderInfo.setOrderStatus(OrderStatusEnum.PAID.getStatus());
        orderInfoService.updateById(orderInfo);

        //3 TODO 调用医院接口，通知医院订单完成支付
    }
}
